package com.example;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import com.google.android.maps.*;

import java.util.List;

public class MapsActivity extends MapActivity {

    double latitude = 39.921009885489916;
    double longitude = 32.85386323928833;
    GeoPoint kizilay = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        MapController controller = mapView.getController();


        controller.animateTo(kizilay);
        controller.setZoom(17);


        mapView.invalidate();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() != 1)
            return false;

        MapView mapView = (MapView) findViewById(R.id.mapView);

        GeoPoint p = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());


        MapOverlay mapOverlay = new MapOverlay(p);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();

        return true;

    }

    class MapOverlay extends com.google.android.maps.Overlay {
        private GeoPoint p;

        public MapOverlay(GeoPoint p) {
            this.p = p;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView,
                            boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                    getResources(), R.drawable.pushpin);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            return true;
        }

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}